package org.benjamin.lending.service.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.benjamin.lending.domain.entity.MemberInfo;
import org.benjamin.lending.domain.entity.Verify;
import org.benjamin.lending.repository.MemberInfoRepository;
import org.benjamin.lending.repository.VerifyRepository;
import org.benjamin.lending.service.VerifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("verifyService")
public class VerifyServiceImpl implements VerifyService{
	
	private final Logger logger = LogManager.getLogger();
	
	@Autowired
	private VerifyRepository verifyRepository;
	
	@Autowired
	private MemberInfoRepository memberInfoRepository;
	
	@Override
	public MemberInfo findByMemberInfoVcode(String vcode){
		Verify verify = this.verifyRepository.findByCode(vcode);
		MemberInfo memberInfo = this.memberInfoRepository.findOne(verify.getUkey());
		if(memberInfo != null){
			logger.warn("查询到的用户信息不存在");
		}
		return memberInfo; 
		
	}

	@Override
	public void resetPassword(Integer userId, String inputPassword) {
		MemberInfo memberInfo = this.memberInfoRepository.findOne(userId);
		String password = DigestUtils.md5Hex(inputPassword);
		memberInfo.setPassword(password);
		this.memberInfoRepository.save(memberInfo);
		logger.info("用户名：{}成功修改了密码，输入的明码是：{}",memberInfo.getUsername(),inputPassword);
	}

}
